package com.mjf.ue.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjf.ue.config.U8Config;
import com.mjf.ue.enums.UEApiEnum;
import com.mjf.ue.util.TimedCacheUtils;
import com.mjf.ue.util.UEHttpUtils;
import com.mjf.ue.vo.BarCodeBatchGetParam;
import com.mjf.ue.vo.BarCodeBatchGetReusultVO;
import com.mjf.ue.vo.U8ProductInListParam;
import com.mjf.ue.vo.U8ProductInListResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/3 14:41
 */
@Slf4j
@Service
public class AsyncUEService {


    @Autowired
    private UEHttpUtils ueHttpUtils;

    /**
     * 获取产成品入库单列表
     *
     * @param param
     * @param isInfo 是否获取详细
     * @return
     */
    public U8ProductInListResultVO getProductInOrderList(U8ProductInListParam param, Boolean isInfo) {
        ObjectMapper objectMapper = new ObjectMapper();
        U8ProductInListResultVO vo = null;
        if (StrUtil.isBlank(param.getPage_index())) {
            param.setPage_index("1");
        }
        if (StrUtil.isBlank(param.getPage_count())) {
            param.setPage_count("1000");
        }
        // 发送GET请求
        String result = ueHttpUtils.sendGet(UEApiEnum.UE_API_PRODUCT_IN_LIST_URL.getUrl(),
            objectMapper.convertValue(param,
            Map.class));
        try {
            vo = objectMapper.readValue(result, U8ProductInListResultVO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!"0".equals(vo.getErrcode())) {
            throw new RuntimeException(vo.getErrmsg());
        }
        assert vo != null;
        int page_count = Integer.parseInt(vo.getPage_count());
        for (int i = 2; i <= page_count; i++) {
            param.setPage_index(String.valueOf(i));
            result = ueHttpUtils.sendGet(UEApiEnum.UE_API_PRODUCT_IN_LIST_URL.getUrl(),
                objectMapper.convertValue(param, Map.class));
            try {
                U8ProductInListResultVO vo1 = objectMapper.readValue(result, U8ProductInListResultVO.class);
                vo.getProductinlist().addAll(vo1.getProductinlist());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 遍历单据列表获取详细
        if (isInfo && CollectionUtil.isNotEmpty(vo.getProductinlist())) {
            for (U8ProductInListResultVO.ProductInfo productInfo : vo.getProductinlist()) {
                String rs = getProductInOrderDetail(productInfo.getCode());
                try {
                    // 组装详细数据
                    U8ProductInListResultVO.U8ProductInListInfoResultVO infoVo = objectMapper.readValue(rs,
                        U8ProductInListResultVO.U8ProductInListInfoResultVO.class);
                    productInfo.setInfo(infoVo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return vo;
    }
    /**
     * 获取产成品入库单详细
     */
    public String getProductInOrderDetail(String code) {
        return ueHttpUtils.sendGet(UEApiEnum.UE_API_PRODUCT_IN_LIST_DETAIL_URL.getUrl(),
            new HashMap<String, Object>() {{
            put("id", code);
        }});
    }

    /**
     * 获取条码档案-批量查询
     */
    public BarCodeBatchGetReusultVO getBarcodeArchive(BarCodeBatchGetParam param) {
        ObjectMapper objectMapper = new ObjectMapper();
        if (ObjectUtil.isNull(param)) {
            param = new BarCodeBatchGetParam();
        }
        int rows_per_page = 1000;
        if (StrUtil.isBlank(param.getPage_index())) {
            param.setPage_index("1");
        }
        if (StrUtil.isBlank(param.getRows_per_page())) {
            param.setRows_per_page(String.valueOf(rows_per_page));
        }
//        param.setCsrccode(code);
//        param.setCinvcname("");
        BarCodeBatchGetReusultVO vo = null;
        // 发送GET请求
        String result = ueHttpUtils.sendGet(UEApiEnum.UE_API_BARCODE_LIST_URL.getUrl(), objectMapper.convertValue(param,
            Map.class));
        log.info("条码档案: {}",result);
        try {
            vo = objectMapper.readValue(result, BarCodeBatchGetReusultVO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        assert vo != null;
        if (!"0".equals(vo.getErrcode())) {
            throw new RuntimeException(vo.getErrmsg());
        }
        int row_count = Integer.parseInt(vo.getPage_count());
        for (int i = 2; i < row_count; i++) {
            param.setPage_index(String.valueOf(i));
            result = ueHttpUtils.sendGet(UEApiEnum.UE_API_BARCODE_LIST_URL.getUrl(), objectMapper.convertValue(param, Map.class));
            try {
                BarCodeBatchGetReusultVO vo1 = objectMapper.readValue(result, BarCodeBatchGetReusultVO.class);
                System.out.println(vo1.getBarcode());
                if ("0".equals(vo1.getErrcode())) {
                    vo.getBarcode().addAll(vo1.getBarcode());
                } else {
                    log.info("获取条码档案失败 {}", result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        if (!"0".equals(vo.getErrcode())) {
//            throw new RuntimeException(vo.getErrmsg());
//        }
        return vo;
    }

}
